"""
@author: 江同学呀
@file: search_as_you_type_field.py
@date: 2024/7/28 0:04
@desc:
    https://www.elastic.co/guide/en/elasticsearch/reference/7.17/search-as-you-type.html

    text-like type for as-you-type completion.
    类似文本的类型，用于键入时完成。
"""
from abc import ABC
from typing import Type, Union, Dict

from espc.common.field_common import IndexOptions, Similarity, TermVector
from espc.orm.model.base.base import _Base
from espc.orm.model.mapping.field.base_field.base_string_field import _BaseStringField
from espc.orm.model.mapping.field.field_attribute import (
    IndexAttribute, IndexOptionsAttribute, NormsAttribute, StoreAttribute, SimilarityAttribute, AnalyzerAttribute,
    SearchAnalyzerAttribute, SearchQuoteAnalyzerAttribute, TermVectorAttribute, MaxShingleSizeAttribute
)
from espc.utils.build_add_attr import build_add_attr


class SearchAsYouTypeField(
    _BaseStringField, MaxShingleSizeAttribute, AnalyzerAttribute, IndexAttribute, IndexOptionsAttribute, NormsAttribute,
    StoreAttribute, SearchAnalyzerAttribute, SearchQuoteAnalyzerAttribute, SimilarityAttribute, TermVectorAttribute, ABC
):
    """
    Search-as-you-type 字段
    """
    _field_type: str = "search_as_you_type"

    def __init__(
            self, field_name: str = None, value: str = None, default: str = None, max_shingle_size: int = None,
            analyzer: str = None, index: bool = None, index_options: Union[str, IndexOptions] = None,
            norms: bool = None, store: bool = None, search_analyzer: str = None, search_quote_analyzer: str = None,
            similarity: Union[str, Similarity] = None, term_vector: Union[str, TermVector] = None
    ):
        super(SearchAsYouTypeField, self).__init__(
            field_name=field_name, value=value, default=default, analyzer=analyzer, norms=norms, index=index,
            index_options=index_options, max_shingle_size=max_shingle_size, similarity=similarity,
            term_vector=term_vector, store=store, search_analyzer=search_analyzer,
            search_quote_analyzer=search_quote_analyzer
        )
        return

    def __get__(self, instance: _Base, owner: Type["SearchAsYouTypeField"]) -> Union["SearchAsYouTypeField", str]:
        return super(SearchAsYouTypeField, self).__get__(instance=instance, owner=owner)

    def __set__(self, instance, value: str):
        super(SearchAsYouTypeField, self).__set__(instance=instance, value=value)
        return

    def __getattribute__(self, item):
        return super(SearchAsYouTypeField, self).__getattribute__(item)

    @build_add_attr
    def _build(self) -> Dict:
        mapping: Dict = {
            "type": self._field_type
        }
        return mapping





